package com.monkeygroup.mygoplus.http;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.monkeygroup.mygoplus.base.BaseModel;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/14.
 */
public class HttpModel {
    private SimpleOnHttpRequestListener mRequestListener;
    protected CustomStringRequest mJsonRequest;
    protected UploadRequest mFileRequest;
    private List<File> files;
    private File file;
    private String fileName;
    protected int mRequestMethod = Request.Method.GET;
    protected String mRequestUrl = "";
    protected Map<String, String> mRequestParams = null;
    protected boolean isTokenSave = false;
    public final static int TIMEOUTMS = 30000;

    /***
     * 网络请求GET方式,token参数默认传true
     ****/
    public HttpModel(String url, SimpleOnHttpRequestListener listener, boolean isTokenSave) {
        this.mRequestListener = listener;
        this.mRequestUrl = url;
        this.isTokenSave = isTokenSave;
    }

    /****
     * 网络请求POST方式,token参数默认传true
     ****/
    public HttpModel(String url, Map<String, String> params, SimpleOnHttpRequestListener listener, boolean isTokenSave) {
        this.mRequestListener = listener;
        this.mRequestUrl = url;
        this.mRequestParams = params;
        this.mRequestMethod = Request.Method.POST;
        this.isTokenSave = isTokenSave;
    }

    /****
     * 上传单个文件或图片资源
     ****/
    public HttpModel(String url, Map<String, String> params, SimpleOnHttpRequestListener listener, String type, File file) {
        this.mRequestListener = listener;
        this.mRequestUrl = url;
        this.mRequestParams = params;
        this.fileName = type;
        this.file = file;
    }

    /****
     * 上传多文件或图片资源
     ****/
    public HttpModel(String url, Map<String, String> params, SimpleOnHttpRequestListener listener, String type, List<File> files) {
        this.mRequestListener = listener;
        this.mRequestUrl = url;
        this.mRequestParams = params;
        this.fileName = type;
        this.files = files;
    }

    /****
     * 普通多表单请求数据(一)
     ****/
    public void startRequest() {
        mJsonRequest = new CustomStringRequest(mRequestMethod, mRequestUrl,
                mRequestParams, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                    BaseModel resultModel = handleResponse(response);
                    Log.e("结果",resultModel.statusCode+"=="+resultModel.statusMessage+"==="+String.valueOf(resultModel.data));
                    mRequestListener.onRequestFinished(Integer.valueOf(resultModel.statusCode), resultModel.statusMessage, resultModel.data);

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                mRequestListener.onRequestFinished(-1, "无法连接服务器", null);
            }
        });

        // 设置网络超时时间
        mJsonRequest.setRetryPolicy(new DefaultRetryPolicy(TIMEOUTMS, 1, 1.0f));
        HttpRequestManager.addHttpRequest(mJsonRequest);
    }

    /****
     * 普通多表单请求数据，返回数据不处理，全部返回(二)
     ****/
    public void Request() {
        mJsonRequest = new CustomStringRequest(mRequestMethod, mRequestUrl,
                mRequestParams, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                mRequestListener.onRequestFinished(0, "", response);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                mRequestListener.onRequestFinished(-1, "无法连接服务器", null);
            }
        });

        // 设置网络超时时间
        mJsonRequest.setRetryPolicy(new DefaultRetryPolicy(TIMEOUTMS, 1, 1.0f));
        HttpRequestManager.addHttpRequest(mJsonRequest);
    }

    /****
     * 上传单个文件
     ****/
    public void uploadFile() {
        mFileRequest = new UploadRequest(mRequestUrl,
                mRequestParams, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                try {
                    BaseModel resultModel = handleResponse(response);
                    mRequestListener.onRequestFinished(Integer.valueOf(resultModel.statusCode), resultModel.statusMessage, resultModel.data);
                } catch (Exception e) {
                    mRequestListener.onRequestFinished(-1, "数据操作异常", null);
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                //此处可以根据返回状态修改
                mRequestListener.onRequestFinished(-1, "无法连接服务器", null);
            }
        }, fileName, file);

        // 设置网络超时时间
        mFileRequest.setRetryPolicy(new DefaultRetryPolicy(TIMEOUTMS, 1, 1.0f));
        HttpRequestManager.addHttpRequest(mFileRequest);
    }

    /*****
     * 上传多文件
     *****/
    public void uploadFiles() {
        mFileRequest = new UploadRequest(mRequestUrl,
                mRequestParams, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                try {
                    BaseModel resultModel = handleResponse(response);

                    mRequestListener.onRequestFinished(Integer.valueOf(resultModel.statusCode), resultModel.statusMessage, resultModel.data.toString());
                } catch (Exception e) {

                    mRequestListener.onRequestFinished(-1, "数据操作异常", null);
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                //此处可以根据返回状态修改
                mRequestListener.onRequestFinished(-1, "无法连接服务器", null);
            }
        }, fileName, files);

        // 设置网络超时时间
        mFileRequest.setRetryPolicy(new DefaultRetryPolicy(TIMEOUTMS, 1, 1.0f));
        HttpRequestManager.addHttpRequest(mFileRequest);
    }

    private BaseModel handleResponse(String jsonString) {
        Gson gson = new Gson();
        Log.e("jsonStringjsonString===",jsonString);
        BaseModel model = gson.fromJson(jsonString, new TypeToken<BaseModel>() {
        }.getType());
        Log.e("result::::", ""+model.getStatusCode()+"=="+model.getData());
        return model;
    }

    public interface SimpleOnHttpRequestListener {
        void onRequestFinished(int resultCode, String resultMsg, Object data);
    }

    /******
     * 取消网络请求
     *****/
    public void cancle() {
        mJsonRequest.cancel();
        mFileRequest.cancel();
    }
}
